package com.example.parkingsharing.ui.components.login

/**
 * PasswordState 继承自 TextFieldState
 * 用于记录密码输入状态
 */
class PasswordState :
    TextFieldState(validator = ::isPasswordValid, errorFor = ::passwordValidationError)

/**
 * 验证密码的输入状态，用于确认第二次输入的密码与第一次输入的是否一致
 */
class ConfirmPasswordState(private val passwordState: PasswordState) : TextFieldState() {
    override val isValid
        get() = passwordAndConfirmationValid(passwordState.text, text)

    override fun getError(): String? {
        return if (showErrors()) {
            passwordConfirmationError()
        } else {
            null
        }
    }
}

/**
 * 验证两次输入的密码是否一致
 */
private fun passwordAndConfirmationValid(password: String, confirmedPassword: String): Boolean {
    return isPasswordValid(password) && password == confirmedPassword
}

/**
 * 密码长度大于6为有效
 */
private fun isPasswordValid(password: String): Boolean {
    return password.length > 6
}

/**
 * 密码长度不符合条件时的错误提示信息
 */
@Suppress("UNUSED_PARAMETER")
private fun passwordValidationError(password: String): String {
    return "Invalid password"
}

/**
 * 两次输入的密码不一致时的错误提示信息
 */
private fun passwordConfirmationError(): String {
    return "Passwords don't match"
}
